#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Version: python2.7
Author: Qiao Da Zhi
Module Description: reusable_activity ... 
File: reusable_activity.py
Date: 2017/10/19
"""
from pyshare.const.enums.activity import ActivityStatus
from pyshare.const.enums.alliance import MapType
from pyshare.utils.time_utils import isPassDay, getCT, get_another_time, has_pass_time
from pyshare.dal.gameconfig.game_env import get_config_env_set


class RoleReusableActivityMixin(object):
    __slots__ = ()
    role_guid = NotImplemented
    activity_type = NotImplemented
    activity_score = NotImplemented
    exchange_num_list = NotImplemented
    buy_num_list = NotImplemented
    refresh_time = NotImplemented
    activity_id = NotImplemented
    activity_point = NotImplemented
    has_box_award_list = NotImplemented
    has_personal_award = NotImplemented

    def replacement_activity_data(self, activity_id, save_immediately=True):
        """新活动数据替换旧活动数据（同一类型活动）"""
        self.exchange_num_list = []
        self.buy_num_list = {}
        self.refresh_time = getCT()
        self.activity_id = activity_id
        self.activity_point = 0
        self.has_box_award_list = []
        self.has_personal_award = 0
        if save_immediately:
            from pyshare.dal.repository_impl import RoleReusableActivityRepository
            RoleReusableActivityRepository.save(self)

    def reset_activity_data(self, save_immediately=True):
        """重置每日数据"""
        if isPassDay(self.refresh_time):
            self.exchange_num_list = []
            self.buy_num_list = {}
            self.refresh_time = getCT()
            if save_immediately:
                from pyshare.dal.repository_impl import RoleReusableActivityRepository
                RoleReusableActivityRepository.save(self)
            return True
        return False

    def adaptation_list(self, max_index, attribute_str):
        """
        适配列表(自适配宝箱领取列表和兑换次数列表)
        :param max_index: 适配的长度
        :param attribute_str: 属性名 宝箱领取列表：has_box_award_list ， 兑换次数列表：exchange_num_list
        :return: 
        """
        role_box_index = len(getattr(self, attribute_str)) - 1
        diff_num = max_index - role_box_index
        if diff_num > 0:
            list_cache = getattr(self, attribute_str)
            list_cache += [0 for i in range(diff_num)]
            setattr(self, attribute_str, list_cache)
            return True
        return False


class ReusableActivityMixin(object):
    __slots__ = ()
    desc_str = NotImplemented
    desc_str_lang = NotImplemented
    activity_name = NotImplemented
    activity_name_lang = NotImplemented
    killed_boss_list = NotImplemented
    personal_range_list = NotImplemented
    activity_type = NotImplemented
    activity_status = NotImplemented
    create_time = NotImplemented
    exchange_item_list = NotImplemented
    activity_id = NotImplemented
    alliance_leader_award_list = NotImplemented
    alliance_member_award_list = NotImplemented
    personal_award_list = NotImplemented
    start_time = NotImplemented
    boss_image_num = NotImplemented
    award_end_time = NotImplemented
    price_list = NotImplemented
    end_time = NotImplemented
    max_num_list = NotImplemented
    boss_id = NotImplemented
    alliance_range_list = NotImplemented

    def asdict_base_info(self):
        return {
            'DescStr': self.desc_str,
            'DescStrLang': self.desc_str_lang,
            'ActivityNameLang': self.activity_name_lang,
            'ActivityType': self.activity_type,
            'ExchangeItemList': self.exchange_item_list,
            'ActivityId': self.activity_id,
            'StartTime': self.start_time,
            'AwardEndTime': self.award_end_time,
            'PriceList': self.price_list,
            'EndTime': self.end_time,
            'MaxNumList': self.max_num_list,
            'KilledBossList': self.killed_boss_list,
            'ActivityStatus': self.activity_status,
            'CreateTime': self.create_time,
            'BossImageNum': self.boss_image_num,
            'BossId': self.boss_id,
            'PersonalRangeList': self.personal_range_list,
            'AllianceRangeList': self.alliance_range_list,
        }

    def asdict_boss(self):
        return {
            'KilledBossList': self.killed_boss_list,
            'BossImageNum': self.boss_image_num,
            'BossId': self.boss_id,
        }

    def asdict_award(self):
        return {
            'PersonalRangeList': self.personal_range_list,
            'AllianceLeaderAwardList': self.alliance_leader_award_list,
            'AllianceMemberAwardList': self.alliance_member_award_list,
            'PersonalAwardList': self.personal_award_list,
            'AllianceRangeList': self.alliance_range_list,
        }

    def generate_box_and_next_boss(self, next_id):
        """boss被击杀生成可领取的box和下一boss id"""
        killed_boss_list_cache = self.killed_boss_list
        killed_boss_list_cache.append(self.boss_id)
        self.killed_boss_list = killed_boss_list_cache
        self.boss_id = next_id

    @property
    def send_time(self):
        # 定时任务发奖时间
        return get_another_time(self.award_end_time, seconds=30)

    @property
    def is_send(self):
        # 是否可发送奖励
        return self.activity_status == ActivityStatus.Open and self.award_end_time <= getCT()

    @property
    def is_expire(self):
        return self.activity_status != ActivityStatus.Open or self.award_end_time <= getCT()

    @property
    def is_expire_clear(self):
        return has_pass_time(self.award_end_time, days=get_config_env_set('ActivityExpireClearTime'))

